package com.baidu.mpks.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.info.GitProperties;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * desc:
 *
 * @author yanmaoyuan
 * @date 2023/10/13
 */
@Slf4j
public final class GitInfoUtils {
    private GitInfoUtils() {}

    /**
     * 输出 Git 版本控制信息.
     *
     * @param context SpringBoot 上下文信息
     */
    public static void print(ConfigurableApplicationContext context) {
        try {
            if (log.isInfoEnabled()) {
                GitProperties gitProperties = context.getBean(GitProperties.class);
                log.info("Git branch: {}", gitProperties.get("branch"));
                log.info("Git build time: {}", gitProperties.get("build.time"));
                log.info("Git commit id: {}", gitProperties.get("commit.id.full"));
                log.info("Git commit user: {}<{}>", gitProperties.get("commit.user.name"), gitProperties.get("commit.user.email"));
                log.info("Git commit time: {}", gitProperties.get("commit.time"));
                log.info("Git commit message: {}", gitProperties.get("commit.message.full"));
            }
        } catch (NoSuchBeanDefinitionException ignored) {
            // ignored
        }
    }
}
